<?php

namespace app\activity\model;

use think\Db;
use think\Model;
use think\Session;

class Project extends Model
{

    protected $project = 'project';
    protected $project_config = 'project_config';

    /**
     * 获取数据
     * @param $status
     * @param $shopId
     * @param $cId
     * @param $page
     * @param $pageSize
     * @return bool|false|\PDOStatement|string|\think\Collection
     */
    public function get_list($status, $shopId, $cId, $page, $pageSize) {
        if (!Session::has('project_status_'.$cId) || Session::get('project_status_'.$cId) !== 1) {
            return false;
        }
        Db::startTrans();
        try{
            if ($status == 1) { //第一次拉数据
                $data = Db::name($this->project)
                    ->where('shop_id',$shopId)
                    ->where('c_id',$cId)
                    ->where('created', '<=', time())
                    ->order('id desc')
                    ->limit(($page-1)*$pageSize,$pageSize)
                    ->select();
                if ($data) {
                    foreach ($data as $k => $v) {
                        $data[$k]['created'] = date("Y-m-d H:i",$v['created']);
                        if ($v['status'] == 1) {
                            Db::name($this->project)->where('id',$v['id'])->update(['status' => 2]);
                        }
                    }
                    return $data;
                }
            } elseif ($status == 2) { //ajax轮询拉数据
                $data = Db::name($this->project)
                    ->where('shop_id',$shopId)
                    ->where('c_id',$cId)
                    ->where('status', 1)
                    ->order('id desc')
                    ->select();
                if ($data) {
                    foreach ($data as $k => $v) {
                        $data[$k]['created'] = date("Y-m-d H:i",$v['created']);
                        Db::name($this->project)->where('id',$v['id'])->update(['status' => 2]);
                    }
                    return $data;
                }
            } else {
                return false;
            }
            Db::commit();
        } catch (\Exception $e) {
            Db::rollback();
            return false;
        }
    }

    /**
     * 发布内容
     * @param $shopId
     * @param $userId
     * @param $cId 投影活动id
     * @param $nickname
     * @param $thumb
     * @param $content
     * @return bool
     */
    public function publish($shopId,$userId,$cId,$nickname,$thumb,$content) {
        if (!Session::has('project_status_'.$cId) || Session::get('project_status_'.$cId) !== 1) {
            return false;
        }
        $insert = [
            'shop_id'   => $shopId,
            'user_id'   => $userId,
            'nickname'  => $nickname,
            'thumb'     => $thumb,
            'content'   => $content,
            'status'    => 1,
            'created'   => time(),
            'c_id'      => $cId
        ];

        $res = Db::name($this->project) -> insert($insert);
        if ($res) {
            return true;
        }
        return false;
    }

    /**
     * 新增投影活动
     * @param $shopId
     * @return bool|int|string
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function add_project($shopId) {
        $data = Db::name($this->project_config)
            ->where('shop_id',$shopId)
            ->find();
        if ($data && $data['status'] == 1) {
            Session::set('project_status_'.$data['id'],1);
            return $data['id'];
        } elseif ($data['status'] != 1) {
            Db::name($this->project_config)->where(['shop_id' => $shopId,'id' => $data['id']])->update(['status' => 1]);
            Session::set('project_status_'.$data['id'],1);
            return $data['id'];
        } else {
            $id = Db::name($this->project_config)->insertGetId(['shop_id'=>$shopId,'created' => time(),'status' => 1]);
            Session::set('project_status_'.$id,1);
            if ($id) {
                return $id;
            }
        }
        return false;
    }

    /**
     * 关闭本场投影活动
     * @param $shopId
     * @param $id
     * @return bool
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function update_project($shopId, $id) {
        $res = Db::name($this->project_config)->where(['shop_id' => $shopId,'id' => $id])->update(['status' => 0,'updated' => time()]);
        Session::delete('project_status_'.$id);
        if ($res) {
            return true;
        }
        return false;
    }

}